package com.eightywork.life.activity;

import java.util.List;
import java.util.Map;

import cn.jpush.android.api.JPushInterface;

import com.eightywork.life.R;
import com.eightywork.life.model.Space;
import com.eightywork.life.service.CacheService;
import com.eightywork.life.service.CpService;
import com.eightywork.life.service.DataService;
import com.eightywork.life.util.MyHttpException;
import com.umeng.analytics.MobclickAgent;

import android.app.AlertDialog;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.SimpleAdapter;

public class IdentityActivity extends BaseActivity implements OnClickListener {
	private ImageButton leftButton;
	private Button okButton;
	private EditText realName, mobile, qiname, dongname, cengname, roomname;
	private LinearLayout tip;
	private AsyncTask task;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.activity_identity);
		initView();
		initData();
	}
	
	@Override
	public void onResume() {		
		super.onResume();
		JPushInterface.onResume(this);
	    MobclickAgent.onResume(this);
	}
	
	@Override
	public void onPause() {
	    super.onPause();
		JPushInterface.onPause(this);
	    MobclickAgent.onPause(this);
	}



	public void initView() {
		leftButton = (ImageButton) findViewById(R.id.button_left);
		okButton = (Button) findViewById(R.id.button_ok);
		realName = (EditText) findViewById(R.id.real_name);
		mobile = (EditText) findViewById(R.id.mobile);
		qiname = (EditText) findViewById(R.id.qiname);
		dongname = (EditText) findViewById(R.id.dongname);
		cengname = (EditText) findViewById(R.id.cengname);
		roomname = (EditText) findViewById(R.id.roomname);
		tip = (LinearLayout) findViewById(R.id.tip_ll);
		leftButton.setOnClickListener(this);
		okButton.setOnClickListener(this);
		qiname.setOnClickListener(this);
		dongname.setOnClickListener(this);
		cengname.setOnClickListener(this);
		roomname.setOnClickListener(this);
	}

	private void initData() {
		Space space = CacheService.getSpace();
		realName.setText(space.getRealname());
		mobile.setText(space.getMobile());
		qiname.setText(space.getQiname());
		dongname.setText(space.getDongname());
		cengname.setText(space.getCengname());
		roomname.setText(space.getRoomname());
		if(space.getVerifystatus().equals("0") || space.getVerifystatus().equals("1")) {
			tip.setVisibility(View.GONE);
			okButton.setText("申请认证");
		} else {
			tip.setVisibility(View.VISIBLE);
			okButton.setText("修改认证");
		}
	}

	@Override
	public void onClick(View v) {
		if (leftButton == v) {
			finish();
		} else if (qiname == v) {
			showQiDialog();
		} else if (dongname == v) {
			showDongDialog((String) qiname.getTag());
		} else if (cengname == v) {
			showCengDialog((String) dongname.getTag());
		} else if (roomname == v) {
			showRoomDialog((String) cengname.getTag());
		} else if (okButton == v) {
			save();
		}
	}

	private void save() {
		String nameString = realName.getEditableText().toString();
		String mobileString = mobile.getEditableText().toString();
		String roomString = roomname.getEditableText().toString();
		if (nameString.equals("")) {
			realName.setError(Html.fromHtml("<font color=red>请填写昵称</font>"));
			return;
		}
		if (mobileString.equals("")) {
			mobile.setError(Html.fromHtml("<font color=red>请填写电话</font>"));
			return;
		}
		if (roomString.equals("")) {
			roomname.setError(Html.fromHtml("<font color=red>请选择房间</font>"));
			return;
		}
		task = new SaveTask().execute(nameString, mobileString, (String) roomname.getTag());
	}

	class SaveTask extends AsyncTask<String, Void, String> {
		@Override
		protected void onPreExecute() {
			showProgressDialog("正在保存信息...");
			super.onPreExecute();
		}

		@Override
		protected String doInBackground(String... params) {
			try {
				CpService.profileVerify(params[0], params[1], params[2]);
				CacheService.saveProfile(CpService.profile());
			} catch (MyHttpException e) {
				return e.getErrorMessage();
			}
			return null;
		}

		@Override
		protected void onPostExecute(String result) {
			hideProgressDialog();
			if (result == null) {
				finish();
			} else {
				showMessage(result);
			}
			super.onPostExecute(result);
		}
	}

	private void showQiDialog() {
		final List<Map<String, String>> list = DataService.getQiList();
		AlertDialog.Builder bulder = new AlertDialog.Builder(context);
		bulder.setTitle("选择期");
		ListView listView = new ListView(context);
		bulder.setView(listView);
		SimpleAdapter adapter = new SimpleAdapter(context, list,
				android.R.layout.simple_list_item_1, new String[] { "qiname" },
				new int[] { android.R.id.text1 });
		listView.setAdapter(adapter);
		listView.setBackgroundColor(Color.WHITE);
		final AlertDialog dialog = bulder.create();
		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View v, int arg2,
					long arg3) {
				qiname.setText(list.get(arg2).get("qiname"));
				qiname.setTag(list.get(arg2).get("qiid"));
				dialog.dismiss();
			}
		});
		dialog.show();
	}

	private void showDongDialog(String qid) {
		final List<Map<String, String>> list = DataService.getDongList(qid);
		AlertDialog.Builder bulder = new AlertDialog.Builder(context);
		bulder.setTitle("选择栋");
		ListView listView = new ListView(context);
		bulder.setView(listView);
		SimpleAdapter adapter = new SimpleAdapter(context, list,
				android.R.layout.simple_list_item_1,
				new String[] { "dongname" }, new int[] { android.R.id.text1 });
		listView.setAdapter(adapter);
		listView.setBackgroundColor(Color.WHITE);
		final AlertDialog dialog = bulder.create();
		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View v, int arg2,
					long arg3) {
				dongname.setText(list.get(arg2).get("dongname"));
				dongname.setTag(list.get(arg2).get("dongid"));
				dialog.dismiss();
			}
		});
		dialog.show();
	}

	private void showCengDialog(String dongid) {
		final List<Map<String, String>> list = DataService.getCengList(dongid);
		AlertDialog.Builder bulder = new AlertDialog.Builder(context);
		bulder.setTitle("选择层");
		ListView listView = new ListView(context);
		bulder.setView(listView);
		SimpleAdapter adapter = new SimpleAdapter(context, list,
				android.R.layout.simple_list_item_1,
				new String[] { "cengname" }, new int[] { android.R.id.text1 });
		listView.setAdapter(adapter);
		listView.setBackgroundColor(Color.WHITE);
		final AlertDialog dialog = bulder.create();
		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View v, int arg2,
					long arg3) {
				cengname.setText(list.get(arg2).get("cengname"));
				cengname.setTag(list.get(arg2).get("cengid"));
				dialog.dismiss();
			}
		});
		dialog.show();
	}

	private void showRoomDialog(String roomid) {
		final List<Map<String, String>> list = DataService.getRoomList(roomid);
		AlertDialog.Builder bulder = new AlertDialog.Builder(context);
		bulder.setTitle("选择房");
		ListView listView = new ListView(context);
		bulder.setView(listView);
		SimpleAdapter adapter = new SimpleAdapter(context, list,
				android.R.layout.simple_list_item_1,
				new String[] { "roomname" }, new int[] { android.R.id.text1 });
		listView.setAdapter(adapter);
		listView.setBackgroundColor(Color.WHITE);
		final AlertDialog dialog = bulder.create();
		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View v, int arg2,
					long arg3) {
				roomname.setText(list.get(arg2).get("roomname"));
				roomname.setTag(list.get(arg2).get("roomid"));
				dialog.dismiss();
			}
		});
		dialog.show();
	}

}
